/**
 * Copyright 2007-2010 非也
 * All rights reserved. 
 * 
 * This library is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License v3 as published by the Free Software
 * Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this library; if not, see http://www.gnu.org/licenses/lgpl.html.
 *
 */
package org.fireflow2.designer.eclipse.figures;

import java.util.List;

import org.eclipse.draw2d.Bendpoint;
import org.eclipse.draw2d.Connection;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.MidpointLocator;
import org.eclipse.draw2d.MouseEvent;
import org.eclipse.draw2d.MouseListener;
import org.eclipse.draw2d.MouseMotionListener;
import org.eclipse.draw2d.PolygonDecoration;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.geometry.Point;
import org.fireflow.pdl.fpdl20.diagram.basic.Line;
import org.fireflow2.designer.eclipse.modelwrapper.basic.TransitionWrapper;

/**
 * 
 * @author 非也 nychen2000@163.com Fire Workflow 官方网站：www.firesoa.com 或者
 *         www.fireflow.org
 * 
 */
public class TransitionFigure extends PolylineConnection {
	TransitionWrapper transitionWrapper = null;
	Label label = null;

	MidpointOffsetLocator locator = null;
	int anchorX = 0;
	int anchorY = 0;

	public TransitionFigure(TransitionWrapper transitionWrapper) {
		this.transitionWrapper = transitionWrapper;
		setTargetDecoration(new PolygonDecoration());
		label = new Label();
		label.setOpaque(true);

		locator = new MidpointOffsetLocator(this, 0);
		add(label, locator);

		
		
		label.addMouseListener(new MyMouseListener());
		label.addMouseMotionListener(new MyMouseMotionListener());

	}

	public void refreshFigure(){
		locator.setOffset(transitionWrapper.getLabelPosition());
		List<Bendpoint> bendPointList = transitionWrapper.getBendpoints();

		String displayName = transitionWrapper.getDisplayName() == null ? "" : transitionWrapper
				.getDisplayName();

		Line line = (Line)transitionWrapper.getDiagramElement().getShape();
		if (org.fireflow.pdl.fpdl20.diagram.basic.Label.CONTENT_FROM_WORKFLOW_ELEMENT.equals(line.getLabel().getText())){
			label.setText(displayName);
		}else{
			label.setText(line.getLabel().getText());
		}
	}
	
	class MyMouseListener implements MouseListener{
		public void mouseDoubleClicked(MouseEvent me) {
		}

		public void mousePressed(MouseEvent me) {
			anchorX = me.x;
			anchorY = me.y;
			me.consume();
		}

		public void mouseReleased(MouseEvent me) {

		}
	}

	class MyMouseMotionListener implements MouseMotionListener {

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.eclipse.draw2d.MouseMotionListener#mouseDragged(org.eclipse.draw2d
		 * .MouseEvent)
		 */
		public void mouseDragged(MouseEvent me) {
			int dx = me.x - anchorX;
			int dy = me.y - anchorY;
			anchorX = me.x;
			anchorY = me.y;
			Point oldPoint = transitionWrapper.getLabelPosition();
			oldPoint.translate(dx, dy);
			transitionWrapper.setLabelPosition(oldPoint);
			me.consume();
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.eclipse.draw2d.MouseMotionListener#mouseEntered(org.eclipse.draw2d
		 * .MouseEvent)
		 */
		public void mouseEntered(MouseEvent me) {
			me.consume();

		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.eclipse.draw2d.MouseMotionListener#mouseExited(org.eclipse.draw2d
		 * .MouseEvent)
		 */
		public void mouseExited(MouseEvent me) {
			me.consume();

		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.eclipse.draw2d.MouseMotionListener#mouseHover(org.eclipse.draw2d
		 * .MouseEvent)
		 */
		public void mouseHover(MouseEvent me) {
			
			me.consume();

		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.eclipse.draw2d.MouseMotionListener#mouseMoved(org.eclipse.draw2d
		 * .MouseEvent)
		 */
		public void mouseMoved(MouseEvent me) {
			me.consume();
		}

	}
}

class MidpointOffsetLocator extends MidpointLocator {
	Point offset = null;

	public MidpointOffsetLocator(Connection c, int i) {
		super(c, i);
		offset = new Point(0, 0);
	}

	@Override
	protected Point getReferencePoint() {
		Point point = super.getReferencePoint();
		return point.getTranslated(offset);
	}

	public Point getOffset() {
		return offset;
	}

	public void setOffset(Point offset) {
		this.offset = offset;
	}

}